package com.treasure.service.collocate.service.impl;

import com.treasure.service.BaseService;
import com.treasure.service.collocate.service.BannerService;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.dto.TBannerDto;
import com.treasure.service.result.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import java.util.List;


/**
 * Banner配置信息
 *
 * @author <a href="mailto:panxinwen@yingu.com">Pan Xinwen</a>
 * @version 1.0 2018-10-30 11:06
 */
@Service
public class BannerServiceImpl extends BaseService implements BannerService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public PageResult<List<TBannerDto>> getBannerList(Integer pageSize, Integer pageNum, Integer status) {

        MultiValueMap<String, String> map = new LinkedMultiValueMap();
        map.set("pageSize",pageSize+"");
        map.set("pageNum",pageNum+"");

        if (!StringUtil.isEmpty(status)){
            map.set("status",status+"");
        }


        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(map, httpHeaders);
        ParameterizedTypeReference<PageResult<List<TBannerDto>>> pt = new ParameterizedTypeReference<PageResult<List<TBannerDto>>>() {
        };
        try {
            ResponseEntity<PageResult<List<TBannerDto>>> resp = restTemplate.exchange(this.getGateway() + "/banner/v1/getBannerList",
                    HttpMethod.POST, formEntity, pt);
            return resp.getBody();
        } catch (HttpClientErrorException e) {
            logger.error("请求方法 "+this.getGateway() + "/banner/v1/getBannerList statusCode : {} error:{} ", e.getStatusCode(), e.getMessage());
        }

        return null;
    }

}
